const http = require('http');
const fs = require('fs');
const path = require('path');

const server = http.createServer((req, res) => {
  const imgPath = path.join(__dirname, '2.png');
  try {
    const fileBuffer = fs.readFileSync(imgPath);
    const base64 = fileBuffer.toString('base64');
    const dataUri = `data:image/png;base64,${base64}`;

    const html = [
      '<!DOCTYPE html>',
      '<html>',
      '<head><meta charset="utf-8"><title>Buffer Image</title></head>',
      '<body style="margin:20px;font-family:sans-serif">',
      `<img style="max-width:480px" src="${dataUri}">`,
      '</body>',
      '</html>'
    ].join('');

    res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
    res.end(html);
  } catch (err) {
    res.statusCode = 500;
    res.end('Image not found. Place 2.png next to this file.');
  }
});

server.listen(8081, () => {
  console.log('server is listening 8081');
});


